package com.matrix.draft.jvm.heap;

public class Test1 {
    public static void main(String[] args) {
        fun2();
    }

    // 测试沾满堆空间
    static void fun1() {
        // 测试占满堆空间
        int i = 0;
        String s = "HelloWorld";
        try {
            while (true) {
                s += s;
                i++;
                System.out.println(i);
            }
        } catch (Exception e) {
            System.out.println(i);
            e.printStackTrace();
        }
    }

    // 字符串大概30次的"+="操作, 也会造成堆满异常
    static void fun2() {
        String s = "Hello";
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
        s += s;
    }


}
